<?php
namespace app\index\controller;
use think\Controller;
use think\Request;
use think\captcha\Captcha;
use think\facade\Session;
use think\facade\Config;
use think\Db;
use \think\facade\Lang;

class Api extends controller
{
  
   /*查询是否存在 %搜索%*/
   public function check_part_number(Request $request){
      $post=$request->post();
      //dump($post);exit;
      $data=Db::table('lily_products')->field('id')->where([['class_id','=',$post['class_id']],['part_number','like',"%".$post['Part_No']."%"]])->find();
      if(empty($data)){
         $result=['status'=>'0','msg'=>'No Result','name'=>'Part_No'];
      }else{
         $result=['status'=>'1'];
      }
      return json($result);
   }

   public function check_part_number2(Request $request){
      $get=$request->get();
      $post=$request->post();

      $pid         = $post['class_id'];
      $part_number = $post['Part_No'];

      $min_class_id=array();
      $min_class_id=class_down($pid,$min_class_id);/*获取到最小分类的id*/
      

      $data=Db::table('lily_products')->field('id')->where([['class_id','in',$min_class_id],['part_number','like',"%$part_number%"]])->select();
      if(empty($data)){
         $result=['status'=>'0','msg'=>'No Result','name'=>'Part_No'];
      }else if(empty($get)){
         $result=['status'=>'1'];
      }else{
         $id_list = array_column($data, 'id');
         $where = [['pid', 'in', $id_list]];
         foreach($get as $key=>$value){
            $key = str_replace('_', ' ', $key);
            $where[] = ['key', '=', "$key"];
            $where[] = ['value', '=', "$value"];
         }
         $data=Db::table('lily_products_detail')->field('id')->where($where)->select();
         if(empty($data)){
            $result=['status'=>'0','msg'=>'No Result','name'=>'Part_No'];
         }else{
            $result=['status'=>'1'];
         }
      }
      return json($result);
   } 
   
   /*列表页加载更多*/
   public function load_more_data(Request $request){
      $get=$request->get();
      $post=$request->post();
      $product = controller('index/Product');

      if(!empty($get)){
         $get=$product->move_ad_and_replace_p($get);
      }

      $get_transition=$get;
      unset($get_transition['page']);

      $page=$post['page']+1;
      $page_start=($page-1)*Config::get('app.list_page_num');

      $max_class_arr=class_uplower($post['class_id']);
      array_pop($max_class_arr);
      $max_class=$max_class_arr[count($max_class_arr)-1]['id'];


      $right_table2=Db::table('lily_right_table')->field('fileds,fileds_alias,seq,is_show')->where('class_id','=',$post['class_id'])->order('seq')->select();
      $right_table1=Db::table('lily_right_table1')->field('fileds,fileds_alias,seq,is_show')->where('class_id','=',$max_class)->order('seq')->select();
      if(!empty($right_table1)){
         $right_table=[];
         foreach ($right_table1 as $key => $value) {
            $findfield=Db::table('lily_products_detail')->where([['key','=',$value['fileds']],['class_id','=',$post['class_id']]])->find();
            if(!empty($findfield)){
               $right_table[]=$value;
            }
         }
      }else{
         $right_table=$right_table2;
      }

      $right_table_orders=Db::table('lily_right_table_order')->field('fileds,seq')->where('class_id','=',$post['class_id'])->order('seq')->select();
      

      $table_data=$product->shu_heng($right_table,$post['class_id'],$right_table_orders,$page_start,$get_transition);
      
     

      foreach ($right_table as $key => $value) {
          if($value['is_show']) {
              $table_limit[] = $value['fileds'];
          }
      }
      
      $new_data=array();
      foreach ($table_data as $key => $value) {

         foreach ($value as $k => $v) {
            if(in_array(str_replace('_',' ',$k),$table_limit)) {
               if(lang::has(trim($v))==true){
                  $new_data[$key][$k]=lang(trim($v));
               }else{
                  $new_data[$key][$k]=$v;
               }
            }
            if($k=='part_number') {
               $new_data[$key]['part_number']=$v;
            }
            if($k=='url_product') {
               $new_data[$key]['url_product']=$v;
            }
            if($k=='price') {
               $new_data[$key]['price']=$v;
            }
         }
      }
      
      
      return $new_data;
   }


   public function reorder_detail_order(){
      $class='735,736,737';
      /*$d_key=Db::table('lily_products_detail')->field('key')->where('class_id','in',$class)->group('key')->select();
      dump($d_key);*/
      
      $arr=array('System of Measurement','Bearing Type','Washers Type','For Load Direction','Construction','Bore Dia','Bore Dia Tolerance','Outer Dia','Outer Dia Tolerance','Outside Dia Shaft Washer','Housing Washer ID','Shaft Washer OD','Height','Height Tolerance','Chamfer Dimension Housing Washer','Chamfer Dimension Shaft Washer','Chamfer Dimension Washer','Inner Dia Housing Washer','Washer Material','Ball Material','Cage Material','Dynamic Thrust Load','Static Thrust Load','Max Speed (X1000 rpm)','Lubrication','Temperature Range','RoHS','REACH','Ball Dia','Ball Qty','Weight');
     
      $products=Db::table('lily_products')->field('id')->where('class_id','in',$class)->select();
      foreach ($products as $key => $value) {
         $detail=Db::table('lily_products_detail')->field('id,key')->where('pid','=',$value['id'])->select();
         foreach ($detail as $k => $v) {
            $seq=array_search($v['key'],$arr,true);
            echo "update lily_products_detail set seq=".$seq." where id=".$v['id'].";<br>";
         }
      }
   }

}    